package org.gwc.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Modifier;

/**
 * 定义四个增强方法，获取目标方法的信息 返回值 异常对象
 * 1、定义方法 -- 增强代码
 * 2、使用注解指定对应的位置
 * 3、配置切点表达式选中方法
 * 4、切面和ioc的配置
 * 5、开启aspectj注解的支持
 *
 * todo: 增强方法中获取目标方法的信息
 *  1、全部增强方法中，获取目标方法的信息（方法名、参数、访问修饰符、所属的类信息。。。。）
 *      JoinPoint joinPoint 包含目标方法的信息
 *  2、返回的结果 -@AfterReturning
 *      Object result 接受返回的结果
 *      @AfterReturning(value = "execution(* org.gwc.service.impl.*.*(..))", returning = "result")
 *  3、异常的信息 -@AfterThrowing
 *      Throwable throwable 接收异常的结果
 *      @AfterThrowing(value = "execution(* org.gwc.service.impl.*.*(..))", throwing = "throwable")
 */

@Component
@Aspect
@Order(10) // 切面优先级，值越低，优先级越高
public class MyAdvice {
    @Before(value = "execution(* org.gwc.service.impl.*.*(..))")
    public void before(JoinPoint joinPoint){
        // 目标方法的类信息
        String simpleName = joinPoint.getTarget().getClass().getSimpleName();
        // 获取方法名称
        int modifiers = joinPoint.getSignature().getModifiers();
        String s = Modifier.toString(modifiers);
        String name = joinPoint.getSignature().getName();
        // 获取参数列表
        Object[] args = joinPoint.getArgs();
        System.out.println("方法开始：args: "+args.toString());
    }

    @AfterReturning(value = "execution(* org.gwc.service.impl.*.*(..))", returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        System.out.println("方法结束：return: ");
    }

    @AfterThrowing(value = "execution(* org.gwc.service.impl.*.*(..))", throwing = "throwable")
    public void afterThrowing(JoinPoint joinPoint,Throwable throwable){
        System.out.println("方法异常：throwable: ");
    }

    @After(value = "execution(* org.gwc.service.impl.*.*(..))")
    public void after(JoinPoint joinPoint){

    }
}
